//
//  FYVisitorView.swift
//  Mircore
//
//  Created by apple on 16/8/8.
//  Copyright © 2016年 飞羽. All rights reserved.
//

import UIKit
import  SnapKit
class FYVisitorView: UIView {

   //延时初始化
    
    var  loginClosure: (() -> ())?
    
    //MARK:-懒加载控件
    
    //旋转图片
    
    private  lazy  var  cycleImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //罩层
    
    private lazy  var maskImageView:UIImageView  = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //主页图片
    
    private  lazy  var iconImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //消息信息
    
    private  lazy var  messageLabel:UILabel  = {
    
        let label  = UILabel()
        
        label.text  = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textColor  = UIColor.darkGrayColor()
        
        label.font = UIFont.systemFontOfSize(14)
        
        //设置文字句居中
        
        label.textAlignment  = .Center
        
        //多行显示
        
        label.numberOfLines  = 0
        
        return  label
        
    
    }()
    
    //注册
    
    private  lazy  var registerButton:  UIButton  = {
    
        let  button  = UIButton()
        
        //添加点击事件
        
        button.addTarget(self, action: #selector(FYVisitorView.registerButtonAction), forControlEvents: .TouchUpInside)
        button.setTitle("注册", forState: .Normal)
        
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        
        //设置字体大小
        
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        //设置背景图片
        
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: .Normal)
        
        return button
     }()
    
    
    //登录
    
    private  lazy var loginButton:UIButton  = {
    
    let  button  = UIButton()
        
        button.addTarget(self, action: #selector(FYVisitorView.loginButtonAction), forControlEvents: .TouchUpInside)
        
        button.setTitle("登录", forState: .Normal)
        
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        
        //设置字体大小
        
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        //设置背景图片
        
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: .Normal)
        
        
      return button
    
     }()
    
    override  init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    
    required  init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has  not been  implemented")
    }
    
    
    //开启动画
    
    private  func  startAnimation() {
    
    //创建核心动画
        
        let  animation  = CABasicAnimation(keyPath: "transform.rotation")
        
        
        //动画的目的
        
        animation.toValue  = 2 * M_PI
        
        //动画时长
        
        animation.duration  = 20
        
        //执行次数
        
        animation.repeatCount  = MAXFLOAT
        
        //动画执行完成不让其释放
        
        animation.removedOnCompletion = false
        
        //执行动画
        
        cycleImageView.layer.addAnimation(animation, forKey: nil)
        
 
    }
    
    //修改访客视图信息
    
    func  updateVisitorViewInfo(message:String?, imageName:String?) {
        
        if  let msg = message ,  imgName = imageName  {
            
         //消息  发现  我的设置的访客视图信息
            
            messageLabel.text  = msg
            
            iconImageView.image = UIImage(named: imgName)
            
            //隐藏旋转图片
            
            cycleImageView.hidden  = true
            
        }else  {
        
        //表示首页要设置的访客视图情况
            
            //显示旋转图片
            
            cycleImageView.hidden  = false
            
            //只有首页开启旋转动画
            
            startAnimation()
        
        }
        
    }
    
    //添加控件设置约束的方法
    
    private func  setupUI()  {
    
    
    //设置背颜色
        
        backgroundColor  = UIColor(white: 237.0  / 255.0, alpha: 1)
        
        //添加控件
        
        addSubview(cycleImageView)
        
        addSubview(maskImageView)
        
        addSubview(iconImageView)
        
        addSubview(messageLabel)
        
        addSubview(registerButton)
        
        addSubview(loginButton)
        
        
        //设置约束
        
        cycleImageView.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
            
         }
        
        maskImageView.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
        
        iconImageView.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
            
        }
        
        messageLabel.snp_makeConstraints { (make) in
            
            make.top.equalTo(cycleImageView.snp_bottom)
            
            make.centerX.equalTo(cycleImageView)
            
            make.width.equalTo(224)
        }
        
        
        registerButton.snp_makeConstraints { (make) in
            
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            
            make.leading.equalTo(messageLabel)
            
            make.height.equalTo(35)
            
            make.width.equalTo(100)
        }
        
        loginButton.snp_makeConstraints { (make) in
            
            make.top.equalTo(registerButton)
            
            make.trailing.equalTo(messageLabel)
            
            make.size.equalTo(registerButton)
            
        }
     
    }
    //MARK:-添加点击事件
    
    @objc  private  func  registerButtonAction() {
     
      //执行闭包
        
        loginClosure?()
    }
    
    @objc  private  func  loginButtonAction() {
    
      //执行闭包
        loginClosure?()
    }

}


extension  FYVisitorView  {

    func text() {
        //使用手写代码的方式自动布局
        
        cycleImageView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        
    }




}
